package cn.lx.auth.secutity.utils;

import cn.hsa.hsaf.core.framework.context.HsafContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @ClassName ApiVerifyUtil
 * @Description: //todo
 * @Author: 落雪
 * @CreateDate: 2025/7/14 23:25
 * @UpdateUser: 落雪
 * @UpdateDate: 2025/7/14 23:25
 * @UpdateRemark:
 * @Version: 1.0
 */
public class ApiVerifyUtil {
    private static Logger log = LoggerFactory.getLogger(ApiVerifyUtil.class);

    public ApiVerifyUtil() {
    }

    public boolean whiteListVerify(String requestUrl) {
        boolean whiteListFlag = false;
        String permits = SpringContextUtils.getApplicationContext().getEnvironment().getProperty("security.oauth2.client.permits");
        if (permits != null) {
            for(int i = 0; i < permits.split(",").length; ++i) {
                String exclusions = permits.split(",")[i];
                if (!"".equals(exclusions) && this.matchWhiteList(exclusions, requestUrl)) {
                    whiteListFlag = true;
                }
            }
        }

        if (this.matchApi(requestUrl)) {
            whiteListFlag = true;
        }

        if ("/error".equals(requestUrl)) {
            whiteListFlag = true;
        }

        return whiteListFlag;
    }

    public boolean access(HttpServletRequest httpRequest) {
        String apiType = SpringContextUtils.getApplicationContext().getEnvironment().getProperty("security.api.type");
        if ("api-mock".equals(apiType)) {
            return true;
        } else {
            String requestUrl = httpRequest.getServletPath().indexOf("?") > 0 ? httpRequest.getServletPath().split("?")[0] : httpRequest.getServletPath();
            String requestMethod = httpRequest.getMethod() == null ? "" : httpRequest.getMethod().toString();
            String queryStr = httpRequest.getQueryString() == null ? "" : httpRequest.getQueryString().toString();
            if ("/error".equals(requestUrl)) {
                return true;
            } else if (this.matchApi(requestUrl)) {
                return true;
            } else {
                String uactId = HsafContextHolder.getContext().getCurrentUser().getUserAcctID();
                new HashMap();

                try {
                    if (httpRequest.getSession().getAttribute("authResu") == null) {
                        log.info("未获取到API权限列表!");
                        return false;
                    } else {
                        Map map = (Map)httpRequest.getSession().getAttribute("authResu");
                        if (this.getMapValue(map, requestUrl)) {
                            return true;
                        } else if ("GET".equals(requestMethod) && !"".equals(queryStr) && this.getMapValue(map, requestUrl + "?" + queryStr)) {
                            return true;
                        } else {
                            return this.matchMapValue(map, requestUrl);
                        }
                    }
                } catch (Exception var9) {
                    log.error(var9.toString(), var9);
                    throw var9;
                }
            }
        }
    }

    private boolean matchMapValue(Map map, String requestUrl) {
        Map<String, String> blackList = new HashMap();
        blackList.put("/**", "1");
        blackList.put("/web/**", "1");
        blackList.put("/**/*", "1");
        blackList.put("/web/**/*", "1");
        blackList.put("/*", "1");
        PathMatcher matcher = new AntPathMatcher();
        Iterator var5 = map.keySet().iterator();

        Object mapKey;
        do {
            if (!var5.hasNext()) {
                return false;
            }

            mapKey = var5.next();
        } while(mapKey.toString().indexOf("*") <= 0 || "1".equals(blackList.get(mapKey.toString())) || !matcher.match(mapKey.toString(), requestUrl));

        return true;
    }

    private boolean getMapValue(Map map, String requestUrl) {
        String resuId = map.get(requestUrl) == null ? "" : map.get(requestUrl).toString();
        if (!"".equals(resuId)) {
            return true;
        } else {
            resuId = map.get("/superAdmin") == null ? "" : map.get("/superAdmin").toString();
            return !"".equals(resuId);
        }
    }

    private boolean matchApi(String requestUrl) {
        PathMatcher matcher = new AntPathMatcher();
        if (matcher.match("/api/**", requestUrl)) {
            return true;
        } else {
            return matcher.match("/*/api/**", requestUrl);
        }
    }

    private boolean matchWhiteList(String WhiteUrl, String requestUrl) {
        PathMatcher matcher = new AntPathMatcher();
        return matcher.match(WhiteUrl, requestUrl);
    }
}
